<?php 

namespace app\admin\controller;
use app\admin\controller\Base;
use app\admin\model\links;
use think\Request;
class Link extends Base{
	//判断是否是POST请求
	public function ispost(){
		if (!Request::instance()->isPost()){
			$this->redirect('index');
		}
	}
	function index(){
		$res = db('links')->order('num', 'dec')->select();
		return $this->fetch('admin/links',['res'=>$res]);
	}
	function linkpostadd(){
		$this->ispost();
		$links = new links;
		$links->num 	=	input('num');
		$links->name	=	input('name');
		$links->url 	=	input('url');
		$links->hidder	=	'n';
		$links->email 		=	input('email');
		$res = $links->save();	
		if($res > 0){
			$data = ["result"=>1, "msg"=>"添加成功"];
			echo json_encode($data);
		}else{
			$data = ["result"=>2, "msg"=>"添加失败"];
			echo json_encode($data);
		}
	}
	function linkstatus(){
		$this->ispost();
		$id = input('id');
		$links = links::get($id)->toArray();
		//判断当前状态
		if ($links['hidder'] == 1) {
			$links = links::get($id);
			$links->hidder = 0;
			$links->save();
			$data = ["result"=>1, "msg"=>"修改成功"];
			echo json_encode($data);
		}else{
			$links = links::get($id);
			$links->hidder = 1;
			$links->save();
			$data = ["result"=>1, "msg"=>"修改成功"];
			echo json_encode($data);
		}
	}
	public function Linkpostedit(){
		$this->isPost();
		$id = input('id');
		$links = links::get($id);
		$links->num 	=	input('newnum');
		$links->name	=	input('newname');
		$links->url 	=	input('newurl');
		$links->email 		=	input('newemail');
		if ($links->save()) {
			$data = ["result"=>1, "msg"=>"编辑成功"];
			echo json_encode($data);
		}else{
			$data = ["result"=>2, "msg"=>"编辑失败"];
			echo json_encode($data);
		}
	}
	public function linkpostdel(){
		$this->isPost();
		$id = input('id');
		$links = links::get($id);
		if($links->delete()){
			$data = ["result"=>1, "msg"=>"删除成功"];
			echo json_encode($data);
		}else{
			$data = ["result"=>2, "msg"=>"删除失败"];
			echo json_encode($data);
		}
	}
}


 ?>